<template>
  <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
          @click="$router.push({ name: 'advert-create'})"
        >添加广告</el-button>
      </div>
     <el-table
      :data="contents"
      border
      style="width: 100%">
      <el-table-column
        label="id"
        prop="id"
        align="center"
       >
      </el-table-column>
      <el-table-column
        align="center"
        label="广告名称"
        prop="name"
        >
      </el-table-column>
      <el-table-column
        align="center"
        label="广告位置"
        prop="adName"
        >
      </el-table-column>
      <el-table-column
        align="center"
        label="广告图片"
        prop="name"
        >
        <template slot-scope="scope">
          <img width="100px" :src="scope.row.img">
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="时间"
        width="250px"
        >
         <template slot-scope="scope">
           <p>开始时间：{{ scope.row.startTimeFormatString }}</p>
          <p>结束时间：{{ scope.row.endTimeFormatString  }}</p>
          </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="上线/下线"
        prop="status">
         <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="onStateChange(scope.row)"
            >
            </el-switch>
          </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作">
        <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
          </template>
      </el-table-column>
    </el-table>
    </el-card>
</template>

<script>
import { getAllSpaces } from '@/services/advert-space'
import { getAdList, updateStatus } from '@/services/advert'
export default {
  name: 'AdvertList',
  data () {
    return {
      contents: [],
      adNameid: []
    }
  },
  created () {
    // 获取所有广告
    this.loadAd()
    // 获取所有广告位
    this.loadAdvertising()
  },
  methods: {
    handleEdit (content) {
      // 设置跳转
      this.$router.push({
        name: 'advert-edit',
        params: {
          id: content.id
        }
      })
    },
    // async loadAdSpace () {
    //   const { data } = await getAllSpaces()
    //   this.advertSpaces = data.content
    // },
    // 上下架状态
    async onStateChange (content) {
      // 点击开关，开启禁用状态
      // this.isLoading = true
      const { data } = await updateStatus({
        id: content.id,
        status: content.status
      })
      if (data.state) {
        this.$message.success(`${content.status === 1 ? '上架' : '下架'}成功`)
        // 取消禁用状态
        // this.isLoading = false
      }
    },
    // 加载广告
    async loadAd () {
      const { data } = await getAdList()
      this.contents = data.content
      console.log(data)
      // 遍历广告所在广告位的id
      for (var key in this.contents) {
        // 遍历所有广告位
        for (var k in this.adNameid) {
          // 检测广告所在广告位的id是否相同
          if (this.adNameid[k].id === this.contents[key].spaceId) {
            // 把广告位名字添加进去
            this.contents[key].adName = this.adNameid[k].name
          }
        }
      }
      this.$store.commit('gai', '广告列表')
    },
    // 加载所有广告位
    async loadAdvertising () {
      const { data } = await getAllSpaces()
      console.log(data)
      this.adNameid = data.content
    }
  }
}
</script>

<style lang="scss" scoped>
.el-table-column {
  text-align: center;
}
</style>
